import Vue from "vue";
import VueRouter from "vue-router";
import Movie from "../views/home/Movie.vue";
//使用路由插件
Vue.use(VueRouter);
//定义路由表
const routes = [
  { path: "/", redirect: "/home" },
  {
    path: "/home",
    name: "Movie",
    component: Movie,
    children: [
      { path: "/home", redirect: "/home/reying" },
      {
        path: "reying",
        name: "reying",
        component: () =>
          import(/* webpackChunkName: "reying" */ "../views/home/reying.vue"),
      },
      {
        path: "yingyuan",
        name: "yingyuan",
        component: () =>
          import(
            /* webpackChunkName: "yingyuan" */ "../views/home/yingyuan.vue"
          ),
      },
      {
        path: "daiying",
        name: "daiying",
        component: () =>
          import(/* webpackChunkName: "daiying" */ "../views/home/daiying.vue"),
      },
      {
        path: "jingdian",
        name: "jingdian",
        component: () =>
          import(
            /* webpackChunkName: "jingdian" */ "../views/home/jingdian.vue"
          ),
      },
    ],
  },
  {
    path: "/Video",
    name: "Video",
    //路由懒加载，作用：可以优化性能，点击后显示这个页面时才加载我们的页面内容
    component: () => import(/* webpackChunkName: "video" */ "../views/Video"),
  },
  {
    path: "/minivideo",
    name: "MiniVideo",
    //路由懒加载，作用：可以优化性能，点击后显示这个页面时才加载我们的页面内容
    component: () =>
      import(/* webpackChunkName: "video" */ "../views/MiniVideo"),
  },
  {
    path: "/show",
    name: "Show",
    //路由懒加载，作用：可以优化性能，点击后显示这个页面时才加载我们的页面内容
    component: () => import(/* webpackChunkName: "video" */ "../views/Show"),
  },
  {
    path: "/me",
    name: "Me",
    //路由懒加载，作用：可以优化性能，点击后显示这个页面时才加载我们的页面内容
    component: () => import(/* webpackChunkName: "video" */ "../views/Me"),
  },
  // 内容页的路由
  {
    path: "/item/:id", //动态路由传参
    name: "Item",
    //路由懒加载，作用：可以优化性能，点击后显示这个页面时才加载我们的页面内容
    component: () =>
      import(/* webpackChunkName: "item" */ "../views/home/Item.vue"),
  },
  {
    //登录路由
    path: "/login",
    name: "Login",
    //路由懒加载，作用：可以优化性能，点击后显示这个页面时才加载我们的页面内容
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/Login.vue"),
  },
  {
    //城市路由
    path: "/city",
    name: "City",
    //路由懒加载，作用：可以优化性能，点击后显示这个页面时才加载我们的页面内容
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/City.vue"),
  },
  {
    //404的路由
    path: "*",
    name: "page404",
    component: () =>
      import(/* webpackChunkName: "page404" */ "../views/Page404.vue"),
  },
];

//创建路由对象
const router = new VueRouter({
  mode: "hash",
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path == "/me") {
    //用户中心，需要守卫
    //需要判断登录状态 ， 我把我们的登录状态 放到了 sessionStorage 中
    if (sessionStorage.getItem("loginType")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

//输出
export default router;
